package cn.cvs.controller;

import cn.cvs.entity.Auction;
import cn.cvs.entity.Auctionrecord;
import cn.cvs.service.AuctionService;
import cn.cvs.service.AuctionrecordService;
import cn.cvs.utils.Page;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 拍卖品控制器
 */
@Controller
public class AuctionController {
    @Resource
    private AuctionService auctionService;
    @Resource
    private AuctionrecordService auctionrecordService;

    @RequestMapping("/SauctionResult")
    public String auctionResult(Model model){
        List<Auction> list1 = auctionService.getList1();  //完成拍卖
        List<Auction> list2=auctionService.getList2();   //正在拍卖
        model.addAttribute("auctionList",list1);
        model.addAttribute("aList",list2);
        return "auctionResult";
    }

    @ResponseBody
    @RequestMapping("Sdel")
    public Object toDel(@RequestParam Integer id) {
        String result = "";
        auctionrecordService.delAuctionrecord(id);
        if (auctionService.delAuction(id) > 0) {
            result = "true";   //删除成功  key  ==  value
        }else {
            result = "false";
        }
        return result;
    }

    @RequestMapping("/SupAuction")
    public String upAuction(Auction auction,HttpServletResponse response, HttpSession session, HttpServletRequest request,
                             @RequestParam(value = "pic",required = false)MultipartFile picPath) throws Exception {
        System.out.println("修改");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        String realPath = request.getSession().getServletContext().getRealPath("/upload");  //上传路径
        System.out.println("上传的路径->"+realPath);
        String originalFile = picPath.getOriginalFilename();   //原文件名
        System.out.println("原文件名:" + originalFile);
        String prefix = FilenameUtils.getExtension(originalFile);  //拿到文件后缀
        System.out.println("原文件后缀:" + prefix);
        auction.setAuctionPic(originalFile);
        auction.setAuctionPicType(prefix);

        System.out.println("实体类数据:"+auction.toString());
        if(auctionService.updAuction(auction)>0){
            FileUtils.copyInputStreamToFile(picPath.getInputStream(),new File(realPath,picPath.getOriginalFilename()));
            System.out.println("上传成功");
            return "redirect:/SrecordList";
        }
        return "upAuction";
    }

    @RequestMapping("/SauctionInfo")
    public String auctionInfo(Integer id,Model model){
        Auction auction = auctionService.getById(id);
        model.addAttribute("auction",auction);
        return "upAuction";
    }

    @RequestMapping("/SaddAuction")
    public String addauction(Auction auction,HttpServletResponse response, HttpSession session, HttpServletRequest request,
                             @RequestParam(value = "pic",required = false)MultipartFile picPath) throws Exception {
        System.out.println("添加");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        String realPath = request.getSession().getServletContext().getRealPath("/upload");  //上传路径
        System.out.println("上传的路径->"+realPath);
        String originalFile = picPath.getOriginalFilename();   //原文件名
        System.out.println("原文件名:" + originalFile);
        String prefix = FilenameUtils.getExtension(originalFile);  //拿到文件后缀
        System.out.println("原文件后缀:" + prefix);
        auction.setAuctionPic(originalFile);
        auction.setAuctionPicType(prefix);

        System.out.println("实体类数据:"+auction.toString());
        if(auctionService.addAuction(auction)>0){
            FileUtils.copyInputStreamToFile(picPath.getInputStream(),new File(realPath,picPath.getOriginalFilename()));
            System.out.println("上传成功");
            return "redirect:/SrecordList";
        }
        return "addAuction";
    }
    @RequestMapping("/StoaddAuction")
    public String toadd(){
        return "addAuction";
    }

    @RequestMapping("/SauctionDetail")
    public String auctionDetail(Integer id,Model model){
        Auction auction = auctionService.getById(id);
        model.addAttribute("auction",auction);
        List<Auctionrecord> list = auctionrecordService.getList(id);
        model.addAttribute("recordList",list);
        return "auctionDetail";
    }

    @RequestMapping("/SrecordList")
    public String recordList(Model model,@RequestParam(defaultValue = "1")Integer pageIndex,String auctionName,
                             String auctionDesc,String auctionStartTime,String auctionEndTime,
                             Integer auctionStartPrice){
        System.out.println("当前页："+pageIndex+"\t\t 名称："+auctionName+"\t\t 描述："+auctionDesc+"\t\t" +
                " 开始时间："+auctionStartTime+"\t\t结束时间："+auctionEndTime+"\t\t 起拍价："+auctionStartPrice);

        Auction auction = new Auction();
        auction.setAuctionName(auctionName!=null?auctionName:"");
        auction.setAuctionDesc(auctionDesc!=null?auctionDesc:"");
        auction.setAuctionStartTime2(auctionStartTime);
        auction.setAuctionEndTime2(auctionEndTime);
        auction.setAuctionStartPrice(auctionStartPrice!=null?auctionStartPrice:0);
        System.out.println(auction.toString());

        int count = auctionService.getCount(auction);
        Page page = new Page(pageIndex,count);
        auction.setIndex(page.getPageIndex());
        auction.setPageSize(page.getPageSize());
        page.setPageList(auctionService.getList(auction));

        model.addAttribute("auctionName",auctionName);
        model.addAttribute("auctionDesc",auctionDesc);
        model.addAttribute("auctionStartTime",auctionStartTime);
        model.addAttribute("auctionEndTime",auctionEndTime);
        model.addAttribute("auctionStartPrice",auctionStartPrice);
        model.addAttribute("auctionList",page.getPageList());
        model.addAttribute("pages",page);
        return "auctionList";
    }



}
/*
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(auctionStartTime != "" && auctionStartTime != null){
            Date das1 = null;
            try {
                das1 = sdf.parse(auctionStartTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            auction.setAuctionStartTime(das1);
        }
        if(auctionEndTime != "" && auctionEndTime != null){
            Date das1 = null;
            try {
                das1 = sdf.parse(auctionEndTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            auction.setAuctionEndTime(das1);
        }
 */